<template>
  <!-- 底部 -->
  <div class="my-footer">
    <!-- 全选 -->
    <div class="custom-control custom-checkbox">
      <input v-model="checkAll" type="checkbox" class="custom-control-input" id="footerCheck">
      <label class="custom-control-label" for="footerCheck">全选</label>
    </div>
    <!-- 合计 -->
    <div>
      <span>合计:</span>
      <span class="price">¥ {{allPrice}}</span>
    </div>
    <!-- 按钮 -->
    <button type="button" class="footer-btn btn btn-primary">结算 ( {{allCount}} )</button>
  </div>
</template>
<script>
export default {
  props:{
    arr: {
      type: Array,
      required: true
    }
  },
  computed:{
    checkAll:{
      set(bool){
        console.log(30,bool);
        // 实现全选功能
        this.arr.forEach(item=>{
          item.goods_state = bool;
        })
      },
      get(){
        // 实现小选影响全选功能
        return this.arr.every(item=>{
          return item.goods_state == true;
        })
      }
    },
    allCount(){
      // 统计用户选中商品的数据
      let result = 0;
      this.arr.forEach(item=>{
        if(item.goods_state){
          // 判断选中商品才需要累加数量
          result+=item.goods_count;
        }
      })
      return result;
    },
    allPrice(){
      let total = 0;
      this.arr.forEach(item=>{
        if(item.goods_state){
          // 注意：先计算单个商品的总价，也就是单价乘以数量，然后再累加
          total += (item.goods_price*item.goods_count)
        }
      })
      return total;
    }
  }
}
</script>

<style lang="less" scoped>
.my-footer {
  position: fixed;
  z-index: 2;
  bottom: 0;
  width: 100%;
  height: 50px;
  border-top: 1px solid #ccc;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 10px;
  background: #fff;

  .price {
    color: red;
    font-weight: bold;
    font-size: 15px;
  }
  .footer-btn {
    min-width: 80px;
    height: 30px;
    line-height: 30px;
    border-radius: 25px;
    padding: 0;
  }
}
</style>